package com.timguan.render.adapter.youa.adapter;

import com.timguan.render.adapter.youa.dao.StockInfoMapper;
import com.timguan.render.adapter.youa.dao.StockInfoQuery;
import com.timguan.render.spi.StockService;
import com.timguan.render.spi.entity.StockInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by guankaiqiang on 16/8/14.
 */
public class StockServiceYouaImpl implements StockService {
    private StockInfoMapper stockInfoMapper;

    public StockServiceYouaImpl(StockInfoMapper stockInfoMapper) {
        this.stockInfoMapper = stockInfoMapper;
    }

    @Override
    public List<StockInfo> getStocks(List<String> itemIds) {
        if (itemIds == null || itemIds.size() == 0) {
            return null;
        }
        StockInfoQuery query = new StockInfoQuery();
        query.createCriteria().andSingleCodeIn(itemIds)
                .andRecordStatusEqualTo("EXIST");
        List<com.timguan.render.adapter.youa.dao.StockInfo> stockInfos = stockInfoMapper.selectByExample(query);
        List<StockInfo> stocks = null;
        if (stockInfos != null && stockInfos.size() != 0) {
            stocks = new ArrayList<StockInfo>(stockInfos.size());
            for (com.timguan.render.adapter.youa.dao.StockInfo stockInfo : stockInfos) {
                StockInfo stock = new StockInfo();
                stock.setItemId(stockInfo.getSingleCode());
                stock.setSoldout(stockInfo.getLocalStock() == null || stockInfo.getSafetyStock() == null || stockInfo.getLocalStock() <= stockInfo
                        .getSafetyStock());
                stock.setStock(stock.isSoldout() ? 0 : stockInfo.getLocalStock() - stockInfo.getSafetyStock());
                stocks.add(stock);
            }
        }
        return stocks;
    }
}
